package cn.bellychang.leetcode.question246;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;

/**
 * A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
 * <p>
 * Write a function to determine if a number is strobogrammatic. The number is represented as a string.
 * <p>
 * For example, the numbers "69", "88", and "818" are all strobogrammatic.
 * <p>
 * 翻转180度后对称的数有：8->8, 0->0, 1->1, 6->9, 9->6，
 *
 * @author ChangLiang
 * @date 2021/3/20
 */
public class Solution {

    public boolean isStrobogrammatic(String num) {

        if (num == null || num.length() == 0) {
            return true;
        }

        char[] chars = num.toCharArray();
        HashMap<Character, Character> map = new HashMap<Character, Character>(chars.length) {
            {
                put('0', '0');
                put('1', '1');
                put('8', '8');
                put('6', '9');
                put('9', '6');
            }
        };

        Deque<Character> sequentialStack = new ArrayDeque<>(chars.length);
        Deque<Character> reversalStack = new ArrayDeque<>(chars.length);


        for (int i = 0; i < chars.length; i++) {
            sequentialStack.push(chars[i]);
            reversalStack.push(chars[chars.length - 1 - i]);
        }

        while (!sequentialStack.isEmpty()) {
            Character c1 = sequentialStack.pop();
            Character c2 = reversalStack.pop();

            if (map.get(c1) == null) {
                return false;
            }

            if (!map.get(c1).equals(c2)) {
                return false;
            }
        }
        return true;
    }
}
